
Partial Class CreateDefaultUsers
    Inherits System.Web.UI.Page

    Protected Sub CreateUsersAndRoles_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateUsersAndRoles.Click

        Dim roleNames() As String = {"Administrator", "FanClubMember", "Manager", "Owner", "Reporter"}
        Dim userNames() As String = {"chrish", "chrisu", "dan", "dave", "jim", "john", "lou"}
        Dim password As String
        Dim CreateStatus As MembershipCreateStatus

        ' create the roles
        For Each roleName As String In roleNames
            If Not Roles.RoleExists(roleName) Then
                Roles.CreateRole(roleName)
            End If
        Next

        ' create the users
        For Each userName As String In userNames
            password = String.Format("{0}@123", userName)

            If Not Membership.ValidateUser(userName, password) Then
                Membership.CreateUser(userName, password, _
                    String.Format("{0}@wroxunited.net", userName), _
                    userName, userName, True, CreateStatus)
                If CreateStatus <> MembershipCreateStatus.Success Then
                    Status.Text = String.Format("Creation of user '{0}' failed: {1}", userName, CreateStatus)
                End If
            End If
        Next

        ' add the users to their correct roles
        Roles.AddUserToRole("chrish", "Reporter")
        Roles.AddUserToRole("chrisu", "Reporter")
        Roles.AddUserToRole("dan", "Manager")
        Roles.AddUserToRoles("dave", roleNames)
        Roles.AddUserToRole("jim", "Owner")
        Roles.AddUserToRole("john", "Reporter")
        Roles.AddUserToRole("lou", "FanClubMember")

        Status.Text = "Users and roles created"

    End Sub
End Class
